package com.theorydance.springsourcestudy.httpmessage.controller;

import com.theorydance.springsourcestudy.httpmessage.entity.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 学习查看http请求的报文内容
 * @Author grand_ranfs
 * @Date 2023/11/23 8:50
 * @Version 1.0
 */
@RestController
public class HttpMessageController {
    @GetMapping("/test02")
    public Map<String,Object> test02(HttpServletRequest request){
        info(request);
        Map<String, Object> result = new HashMap<>();
        return result;
    }
    @GetMapping("/test03")
    public Map<String,Object> test03(HttpServletRequest request, String name){
        info(request);
        Map<String, Object> result = new HashMap<>();
        result.put("name", name);
        return result;
    }
    @GetMapping("/test031")
    public Map<String,Object> test031(
            HttpServletRequest request,
            @RequestParam(value="name", required = false) String name){
        info(request);
        Map<String, Object> result = new HashMap<>();
        result.put("name", name);
        return result;
    }

    @PostMapping("/test04")
    public Map<String,Object> test04(HttpServletRequest request, @RequestBody User user){
        // 在这种情况下，@RequestBody已经读取过流并已经关闭，在info中不能再次读取流数据了
        info(request);
        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        return result;
    }

    /**
     * 对http的请求头报文进行输出
     * @Author grand_ranfs
     * @Date 2023/11/23 8:56
     * @param: request
     */
    public void info(HttpServletRequest request) {
        try {
            Enumeration<String> headerNames = request.getHeaderNames();
            System.out.println("----------------http request message-------------------");
            System.out.println("----------------request headers-------------------");
            while (headerNames.hasMoreElements()) {
                String key = headerNames.nextElement();
                System.out.println(key + " = " + request.getHeader(key));
            }
            System.out.println("----------------request query-------------------");
            System.out.println(request.getQueryString());

            System.out.println("----------------request content info-------------------");
            System.out.println("Content-Type = " + request.getContentType());
            System.out.println("Content-Length = " + request.getContentLength());

            if (request.getContentLength() != -1) {
                System.out.println("----------------request body-------------------");
                byte[] buffer = new byte[request.getContentLength()];
                request.getInputStream().read(buffer);
                System.out.println(new String(buffer));
            }

            System.out.println("----------------end-------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
